﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CraigsCrawler
{
    /// <summary>
    /// Interaction logic for EditSearchStorage.xaml
    /// </summary>
    public partial class EditSearchStorage : Window
    {
        private MainWindow mainWindow;
        public SearchStorage Storage { get; set; }

        public EditSearchStorage(MainWindow mainWindow, SearchStorage storage)
        {
            InitializeComponent();

            this.mainWindow = mainWindow;
            Storage = storage;

            if (Storage == null)
            {
                SearchStorageName.IsEnabled = true;
                OKButton.Content = "Create";
            }
            else
            {
                SearchStorageName.Text = storage.Name;
                Query.Text = storage.query;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (Storage == null)
            {
                if (string.IsNullOrWhiteSpace(SearchStorageName.Text))
                {
                    MessageBox.Show("Search must contain a name", "Name empty", MessageBoxButton.OK, MessageBoxImage.Stop);
                    return;
                }

                Storage = SearchStorage.Load(SearchStorageName.Text);
            }

            Storage.query = Query.Text;
            Storage.Save();
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start(Query.Text);
        }

        private void CopyButton_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(Query.Text);
        }

        private void PasteButton_Click(object sender, RoutedEventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                string text = Clipboard.GetText().Trim();

                if (!text.Contains('\n'))
                {
                    Query.Text = text;
                }
            }
        }

        private void Scrub_Click(object sender, RoutedEventArgs e)
        {
            List<Entry> entries = Storage
                .entries
                .OrderBy(en => en.entryState, new EntryStateComparer_RelevantFirst())
                .ThenBy(en => en.lastRetrieved)
                .ToList();

            foreach (Entry entry in entries)
            {
                Querier.Current.QueueRequest(entry.link, entry, (u, c) => mainWindow.ProcessedScrub(entry, u, c), (u, ex) => MainWindow.WriteException("Failed scrub: " + u, ex));
            }

            MainWindow.WriteLine("Queued " + entries.Count + " entries for scrub");
        }

        private void Empty_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.OK == MessageBox.Show("This will empty this search of all existing entries. Are you sure?", "Empty search", MessageBoxButton.OKCancel, MessageBoxImage.Warning))
            {
                Storage.Empty();
            }
        }

        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBoxResult.OK == MessageBox.Show("This will empty this search of all existing entries. Are you sure?", "Empty search", MessageBoxButton.OKCancel, MessageBoxImage.Warning))
            {
                Storage.Delete();
                Storage = null;
                Close();
            }
        }
    }
}
